home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / win / rtfhelp.zip / RTFHMAIN.C < prev    next >
Text File  |  1994-11-06  |  8KB  |  303 lines

  1. /*
  2. ======================================================================
  3.    RTFHelp Windows Help Generation Tool
  4.    (C) Copyright 1994 by J. Hlavaty
  5.  
  6.    RTFHMain.c
  7.    Parses arguments, opens files and calls text parsing routine
  8. ======================================================================
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "rtfhelp.h"
  15.  
  16. struct COMMANDENTRY CommandList[NOOFCMDS] =
  17. {
  18.   "BOLD",        bold,
  19.   "BMC",         bitmapcharacter,
  20.   "BOX",         box,
  21.   "BROWSESEQ",   browseseq,
  22.   "BUILD",       build,
  23.   "CENTER",      centeralign,
  24.   "CONTEXTID",   contextID,
  25.   "DEFFONT",     deffont,
  26.   "DEFINELINK",  definelink,
  27.   "DEFINEPOPUP", definepopup, // older term
  28.   "DEFINITION",  putpopup,    // older term
  29.   "DEFTAB",      deftab,
  30.   "FIRSTINDENT", firstlineindent,
  31.   "FONT",        font,
  32.   "FONTSIZE",    fontsize,
  33.   "JUST",        justified,
  34.   "ITALIC",      italic,
  35.   "KEEP",        keep,
  36.   "KEEPNEXT",    keepnext,
  37.   "KEYWORD",     keyword,
  38.   "LEFT",        leftalign,
  39.   "LEFTINDENT",  leftindent,
  40.   "LINE",        requiredlinebreak,
  41.   "LINKCONTEXT", definelink,  // older term
  42.   "PAGE",        newpage,
  43.   "PARA",        newparagraph,
  44.   "PARD",        pard,
  45.   "PLAIN",       plain,
  46.   "PUTLINK",     putlink,
  47.   "PUTPOPUP",    putpopup,
  48.   "RIGHT",       rightalign,
  49.   "RIGHTINDENT", rightindent,
  50.   "SA",          spaceafter,
  51.   "SB",          spacebefore,
  52.   "SL",          spacebetween,
  53.   "SMALLCAPS",   smallcaps,
  54.   "SPACECHAR",   spacechar,
  55.   "TAB",         tab,
  56.   "TB",          tabbar,
  57.   "TQC",         tabcenter,
  58.   "TQDEC",       tabdec,
  59.   "TQR",         tabright,
  60.   "TITLE",       title,
  61.   "TX",          tabpos,
  62.   "XREF",        putlink,     // older term
  63.   "XREFCONTEXT", definepopup,
  64.   "XREFID",      xrefID,
  65.   "*",           comment,
  66.   ";",           comment,
  67.   //--- undocumented tags follow
  68.   "DEFFORMAT",   defformat,
  69.   "MARGB",       marginbottom,
  70.   "MARGL",       marginleft,
  71.   "MARGR",       marginright,
  72.   "MARGT",       margintop,
  73. } ;
  74.  
  75. int main( int argc, char **argv )
  76. {
  77.   FILE  *flInput, *flOutput ;
  78.   int    rc ;
  79.   int    iLine = 1 ; // what line of the input file we've processed
  80.  
  81.   // output logo information to user
  82.   DisplayBanner() ;
  83.  
  84.   // process user parameters (passed into main)
  85.   ProcessArgs( argc, argv ) ;
  86.  
  87.   // if no input file then there's nothing to do...
  88.   if ( NULL == pszInput )
  89.   {
  90.      return INVALID_NO_OF_PARAMS ;
  91.   }
  92.  
  93.   // szBuffer is a temporary holding area -- copy file name
  94.   //   and add extension if no extension already given
  95.   strcpy( szBuffer, pszInput ) ;
  96.   if ( !strchr( szBuffer, '.' ) )
  97.   {
  98.      strcat( szBuffer,".hdc" ) ;
  99.   }
  100.  
  101.   // open input file, exit if not found...
  102.   if ( NULL == ( flInput = fopen( szBuffer,"r" ) ) )
  103.   {
  104.      printf( "Could not open input file '%s'\n", szBuffer ) ;
  105.      return FOPEN_RETURNED_NULL ;
  106.   }
  107.  
  108.   // generate output file name if none is given by user
  109.   if ( NULL == pszOutput )
  110.   {
  111.      char *pos ;
  112.  
  113.      // copy input file name again to buffer
  114.      strcpy( szBuffer, pszInput ) ;
  115.      pszOutput = szBuffer ;
  116.  
  117.      // remove the file extension (if any) by nulling out the period delimiter
  118.      pos = strchr( pszOutput,'.' ) ;
  119.      if (pos)
  120.         *pos='\0' ;
  121.   }
  122.   else
  123.   {
  124.      strcpy( szBuffer, pszOutput ) ;
  125.   }
  126.  
  127.   // if no extension given on output file, add .RTF...
  128.   if ( !strchr( szBuffer,'.' ) )
  129.   {
  130.      strcat( szBuffer,".rtf" ) ;
  131.   }
  132.  
  133.   // if open of output file is not NULL, then file exists...
  134.   if ( NULL != ( flOutput = fopen( szBuffer,"r" ) ) )
  135.   {
  136.      fclose ( flOutput ) ;  // close current open
  137.  
  138.      // warn user if they haven't asked us not to
  139.      if ( FALSE == bOverwrite )
  140.      {
  141.         // we use szBuffer2 to not overwrite our file name in szBuffer
  142.         sprintf( szBuffer2,"Output file '%s'already exists, overwrite? (Y/N) ",
  143.                   szBuffer ) ;
  144.         fputs( szBuffer2, stderr ) ;
  145.         gets( szBuffer2 ) ;
  146.  
  147.         if ( 'Y' != toupper( *szBuffer2 ) )
  148.         {
  149.            fputs( "\t*** Table generation canceled\n", stderr ) ;
  150.            return USER_ABORT ;
  151.         }
  152.         else
  153.         {
  154.            fputs( "\t*** Table replaced\n", stderr ) ;
  155.         }
  156.      }
  157.   }
  158.  
  159.   // try to open output file for write, exit if can't open output file
  160.   if ( NULL == ( flOutput = fopen( szBuffer,"w" ) ) )
  161.   {
  162.      printf( "Could not open output file '%s'\n", szBuffer ) ;
  163.      return FOPEN_RETURNED_NULL ;
  164.   }
  165.  
  166. // generating HDC files from RTF files not in this release of RTFHelp
  167.   if ( 0 )
  168.   {
  169. /*
  170.  * if ( TRUE == bGeneratingHDC ) {
  171.  *   // this is the code for generating...
  172.  *   JH -- stubbed out of version 2.0...
  173.  *   DoGeneration( flInput, flOutput ) ;
  174.  *   DoSecondGeneration( &flInput, &flOutput ) ;
  175. */
  176.   }
  177.   else
  178.   {
  179.     // write header -- RTF group and font table
  180.     write_head( flOutput ) ;
  181.  
  182.     // add to output file by parsing input file
  183.     while( TRUE )
  184.     {
  185.       if ( NULL == fgets( szBuffer,RTFH_BUFFERSIZE,flInput ) )
  186.       {
  187.          if ( feof(flInput) )
  188.          {
  189.             break ;           // EOF -- we're finished
  190.          }
  191.          else
  192.          {  // some error
  193.             printf("RTFHELP:  'fgets' error\n") ;
  194.             return LIBRARY_ERROR ;
  195.          } // end IF #2
  196.       } // end IF #1
  197.  
  198.       // generate RTF file for current line of input file...
  199.       rc = Tokenize( flInput, flOutput, szBuffer ) ;
  200.  
  201.       if ( rc != RC_OK )
  202.       {
  203.          printf("RTFHELP:  error processing line '%d'\n",iLine) ;
  204.       }
  205.  
  206.       iLine++ ;
  207.  
  208.     } // end WHILE loop
  209.  
  210.     // close the RTF group
  211.     write_tail(flOutput) ;
  212.   }
  213.  
  214.   // close our output and input files
  215.   fclose(flOutput) ;
  216.   fclose(flInput) ;
  217.   exit(RC_OK) ;
  218. }
  219.  
  220. write_head( FILE *File )
  221. {
  222.   char *Text = "{\\rtf1\\ansi " ;
  223.   char *Fonts = "\n\\deff0{\\fonttbl\
  224. {\\f0\\froman Tms Rmn;}\
  225. {\\f1\\fdecor Symbol;}\
  226. {\\f2\\fswiss Helv;}\
  227. {\\f3\\fmodern pica;}\
  228. {\\f4\\fmodern Courier;}\
  229. {\\f5\\fmodern elite;}\
  230. {\\f6\\fmodern prestige;}\
  231. {\\f7\\fmodern lettergothic;}\
  232. {\\f8\\fmodern gothicPS;}\
  233. {\\f9\\fmodern cubicPS;}\
  234. {\\f10\\fmodern lineprinter;}\
  235. {\\f11\\fswiss Helvetica;}\
  236. {\\f12\\fmodern avantegarde;}\
  237. {\\f13\\fmodern spartan;}\
  238. {\\f14\\fmodern metro;}\
  239. {\\f15\\fmodern presentation;}\
  240. {\\f16\\fmodern APL;}\
  241. {\\f17\\fmodern OCRA;}\
  242. {\\f18\\fmodern OCRB;}\
  243. {\\f19\\froman boldPS;}\
  244. {\\f20\\froman emperorPS;}\
  245. {\\f21\\froman madaleine;}\
  246. {\\f22\\froman zapf humanist;}\
  247. {\\f23\\froman classic;}\
  248. {\\f24\\froman roman f;}\
  249. {\\f25\\froman roman g;}\
  250. {\\f26\\froman roman h;}\
  251. {\\f27\\froman timesroman;}\
  252. {\\f28\\froman century;}\
  253. {\\f29\\froman palantino;}\
  254. {\\f30\\froman souvenir;}\
  255. {\\f31\\froman garamond;}\
  256. {\\f32\\froman caledonia;}\
  257. {\\f33\\froman bodini;}\
  258. {\\f34\\froman university;}\
  259. {\\f35\\fscript Script;}\
  260. {\\f36\\fscript scriptPS;}\
  261. {\\f37\\fscript script c;}\
  262. {\\f38\\fscript script d;}\
  263. {\\f39\\fscript commercial script;}\
  264. }\n" ;
  265.   int   rc ;
  266.  
  267.   // write RTF file header info
  268.   rc = fprintf( File, "%s", Text ) ;
  269.   if ( rc < 0 )
  270.   {
  271.      printf( "RTFHELP:  error from fprintf\n" ) ;
  272.      exit( LIBRARY_ERROR ) ;
  273.   }
  274.  
  275.   // write font table
  276.   rc = fprintf( File, "%s", Fonts ) ;
  277.   if ( rc < 0 )
  278.   {
  279.      printf( "RTFHELP:  error from fprintf\n" ) ;
  280.      exit( LIBRARY_ERROR ) ;
  281.   }
  282.  
  283.   return RC_OK ;
  284. }
  285.  
  286. write_tail( FILE *File )
  287. {
  288.   char *Text = "}\n" ;
  289.  
  290.   int   rc ;
  291.  
  292.   // close the RTF group
  293.   rc = fprintf( File, "%s", Text ) ;
  294.   if ( rc < 0 )
  295.   {
  296.      printf( "RTFHELP:  error from fprintf\n" ) ;
  297.      exit( LIBRARY_ERROR ) ;
  298.   }
  299.  
  300.   return RC_OK ;
  301. }
  302.  
  303.